﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Forums
{
    public partial class RegistrationPage : System.Web.UI.Page
    {
        string connection;
        SqlConnection sqlConnection;
        SqlCommand sqlUserCheck;

        protected void Page_Load(object sender, EventArgs e)
        {
            connection = WebConfigurationManager.ConnectionStrings["CentForumsDB"].ConnectionString;
            sqlConnection = new SqlConnection(connection);

            //update the menuContentHolder
            Master.FindControl("menuContentHolder").Controls.Clear();

        }

        public void userCheck(object sender, EventArgs e)
        {                                    
            sqlConnection.Open();

            TextBox txtUserName = (TextBox)registrationForm.FindControl("txtUser");
            TextBox txtPassword = (TextBox)registrationForm.FindControl("txtPassword");
            Label lblUserCheck = (Label)registrationForm.FindControl("lblUser");

            lblUserCheck.Visible = false;
      
            string sqlCommand = "SELECT COUNT(1) FROM Registration WHERE member_name = '" + txtUserName.Text + "'";
            sqlUserCheck = new SqlCommand(sqlCommand, sqlConnection);

            int count = Convert.ToInt32(sqlUserCheck.ExecuteScalar());
            sqlConnection.Close();

            if (count > 0)
            {
                lblUserCheck.Visible = true;
                txtUserName.Focus();
            }
            else
            {
                txtPassword.Focus();
            }
                     
        }

        public void requiredFields(object sender, ServerValidateEventArgs e)
        {
            TextBox txtUserName = (TextBox)registrationForm.FindControl("txtUser");
            TextBox txtPassword = (TextBox)registrationForm.FindControl("txtPassword");
            TextBox txtPasswordCheck = (TextBox)registrationForm.FindControl("txtPassCheck");
            TextBox txtFName = (TextBox)registrationForm.FindControl("txtFName");
            TextBox txtLName = (TextBox)registrationForm.FindControl("txtLName");
            TextBox txtEmail = (TextBox)registrationForm.FindControl("txtEmail");
            Label lblInfo = (Label)registrationForm.FindControl("lblInfo");

            if (txtUserName.Text == "" || txtPassword.Text == "" || txtPasswordCheck.Text == "" ||
                txtFName.Text == "" || txtLName.Text == "" || txtEmail.Text == "")
            {
                e.IsValid = false;
            }
        }

        public void btnCancel(object sender, EventArgs e)
        {
            
            Response.Redirect("HomePage.aspx");
        }

        protected void registrationForm_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            Response.Redirect("HomePage.aspx");
        }
    }
}
